package com.wwinsoft.framework.entity.security;

import java.util.ArrayList;
import java.util.List;


import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.wwinsoft.framework.entity.IdVersionEntity;
import com.wwinsoft.modules.utils.reflection.ConvertUtils;

@Entity
@Table(name = "ACCT_RESOURCE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnoreProperties(value = { "parent", "hibernateLazyInitializer", "handler","authorityList","getAuthNamesArray" })
public class Resource extends IdVersionEntity {

    public final static String  RESOURCE_TYPE_URL = "1";
    public final static String  RESOURCE_TYPE_SQL = "2";

    private String title;
    private double position;
    private String resourceType;
    private String url;
    private String limitSql;
    private List<Authority> authorityList = new ArrayList<Authority>();

    public Resource() {
    }

    public double getPosition() {
        return position;
    }

    public void setPosition(double position) {
        this.position = position;
    }

    public String getResourceType() {
        return resourceType;
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getLimitSql() {
        return limitSql;
    }

    public void setLimitSql(String limitSql) {
        this.limitSql = limitSql;
    }

    /**
     * 可访问该资源的授权集合.
     */
    @ManyToMany
    @JoinTable(name = "ACCT_RESOURCE_AUTHORITY", joinColumns = { @JoinColumn(name = "RESOURCE_ID") }, inverseJoinColumns = { @JoinColumn(name = "AUTHORITY_ID") })
    @Fetch(FetchMode.SELECT)
    @OrderBy("id")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public List<Authority> getAuthorityList() {
        return authorityList;
    }

    public void setAuthorityList(List<Authority> authorityList) {
        this.authorityList = authorityList;
    }

    @Transient
    public  String getMethod(){
        return null;
    }

    @Transient
    public String[] getAuthNamesArray() {
        return (String[]) ConvertUtils.convertElementPropertyToList(authorityList, "prefixedName").toArray(new String[authorityList.size()]);
    }

}
